package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.domain.Setmeal;
import com.example.dto.SetmealDto;
import com.example.result.JsonResult;
import com.example.service.SetmealDishService;
import com.example.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController extends BaseController {  //套餐
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;

    //新增套餐
    @PostMapping
    public JsonResult save(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithSetmeal(setmealDto);
        return new JsonResult(1,"新增成功");
    }
    //套餐分页查询
    @GetMapping("/page")
    public JsonResult page(Integer page,Integer pageSize,String name){
        IPage<SetmealDto> setmealDtoIPage=new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(name!=null,Setmeal::getName,name);
        queryWrapper.orderByAsc(Setmeal::getUpdateTime);
        setmealService.getSetmealDtoPage(setmealDtoIPage,queryWrapper);
        return new JsonResult(1,"",setmealDtoIPage);
    }
    //删除套餐
    @DeleteMapping
    public JsonResult delete(@RequestParam List<Long> ids){
        setmealService.removeWithDish(ids);
        return new JsonResult(1,"删除成功");
    }
    //根据条件查询套餐数据
    @GetMapping("/list")
    public JsonResult list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);

        return new JsonResult(1,"",list);
    }
}

